<%@page language="C#" Debug="true" %>
<%@import namespace="My.Data" %>

<script runat="server">

// Name: 流量统计器服务器端
// Version: 0.0.0.0
// Copyright: ProgrammingCanRuinYourLife.cn

// 入口
protected void Page_Load (object sender, System.EventArgs e)
{
    //UrlReferrer就是要统计的页面
    if (  null == Request.UrlReferrer  ){
        throw new Exception ("此页面不能直接访问！");    // 这里最好就是变成日志记录,而不是抛出错误。
        Response.End();
        return;
    }

    string connStr = ConfigurationManager.ConnectionStrings["myComponent"].ConnectionString.ToString();
    IDB db = new DbSqlServerDebug(connStr);
    string tbName = "My_Stat_Data";

    Collecting col = new Collecting(db,tbName);
    col.SaveStatData( col.GetStatData() );
    //Response.Write("alert(\"" + col.SaveStatData( col.GetStatData() ) + "\");");
}

public class Collecting
{
    // 成员数据
    private IDB _db = null;
    private string _tbName = "";

    // asp.net对象
    private HttpRequest Request = HttpContext.Current.Request;



    // 构造函数
    public Collecting( IDB db, string tbName )
    {
        _db = db;
        _tbName = tbName;
    }

    /**
     * 此函数返回 统计Get提交的数据
     */
    public StatInfo GetStatData()
    {
        StatInfo info = new StatInfo();

        info.CreatedTime = DateTime.Now.ToString();
        info.PageURL     = Request.UrlReferrer.ToString();
        info.ClientIP    = Request.UserHostAddress;
        info.ClientUserAgent   = Request.UserAgent;

        info.ClientID = Request.QueryString["clientID"];
        info.ItemID   = Request.QueryString["itemID"];
        info.PageTitle= Request.QueryString["title"];
        info.ReferrerURL = Request.QueryString["referrer"];
        info.ClientScreenWidth = Request.QueryString["screenWidth"];
        info.ClientScreenHeight= Request.QueryString["screenHeight"];

        return info;
    }



    /**
     * 持久保存统计数据
     */
    public void SaveStatData( StatInfo info )
    {
        //
        string sql = string.Format(@"Insert Into {0:tbName}(createTime,clientID,itemID,pageURL,pageTitle,referrerURL,clientIP,clientUserAgent,clientScreenWidth,clientScreenHeight)
                                                     Values('{1}',{2},{3},'{4}','{5}','{6}','{7}','{8}',{9},{10})",
                                                     _tbName,info.CreatedTime,info.ClientID,info.ItemID,info.PageURL,info.PageTitle,info.ReferrerURL,
                                                     info.ClientIP,info.ClientUserAgent,info.ClientScreenWidth,info.ClientScreenHeight);

        //HttpContext.Current.Response.Write("alert(\"" + sql + "\");");
        try{ _db.ExecuteNonQuery(sql); }
        finally{}
    }
}

// 信息实体类
public class StatInfo
{
    private string _createdTime;
    private string _clientID;
    private string _itemID;
    private string _pageURL;
    private string _pageTitle;
    private string _referrerURL;
    private string _clientIP;
    private string _userAgent;
    private string _screenWidth;
    private string _screenHeight;

    public string CreatedTime {get{return _createdTime; }set{_createdTime = value;}}
    public string ClientID    {get{return _clientID;    }set{_clientID    = value;}}
    public string ItemID      {get{return _itemID;      }set{_itemID      = value;}}
    public string PageURL     {get{return _pageURL;     }set{_pageURL     = value;}}
    public string PageTitle   {get{return _pageTitle;   }set{_pageTitle   = value;}}
    public string ReferrerURL {get{return _referrerURL; }set{_referrerURL = value;}}
    public string ClientIP    {get{return _clientIP;    }set{_clientIP    = value;}}
    public string ClientUserAgent   {get{return _userAgent;   }set{_userAgent   = value;}}
    public string ClientScreenWidth {get{return _screenWidth; }set{_screenWidth = value;}}
    public string ClientScreenHeight{get{return _screenHeight;}set{_screenHeight= value;}}

}


/** 测试

        StatInfo info = new StatInfo();

        info.CreatedTime = DateTime.Now.ToString();
        info.PageURL     = "safsafsa";
        info.ClientIP    = "dsfas";
        info.ClientUserAgent   = "sdfsdfds";

        info.ClientID = "10000";
        info.PageTitle= "中文啊";
        info.ReferrerURL = "fsdfs";
        info.ClientScreenWidth = "1024";
        info.ClientScreenHeight= "768";
*/
</script>